Ext.define('vacake.controller.ShoppingBalance', {
    extend: 'Ext.app.Controller',

    config: {
        refs: {
            mainView: 'mainView',
            usersView: 'usersView',
            shoppingBalanceView: 'shoppingBalanceView',
            backButton: 'shoppingBalanceView #back_Shopping',
            showCartInfo: 'shoppingBalanceView #showCartInfo',
            indentSubmit: 'shoppingBalanceView #indentSubmit',
            paymentTypes: 'shoppingBalanceView radiofield[name=paymenttype]',
            ordersFrom: 'shoppingBalanceView #ordersFrom'
        },
        control: {
            backButton: {
                tap: function () {

                    Ext.Viewport.remove(this.getShoppingBalanceView(), true);
                    var backid = Ext.Viewport.getInnerItems().length - 2;
                    Ext.Viewport.setActiveItem(backid);
                }
            },
            indentSubmit: {
                tap: function () {
                    Ext.Viewport.mask({
                        xtype: 'loadmask',
                        message: '提交订单中...'
                    });
                    var orderer = Ext.getCmp('orderer').getValue();
                    var otel = Ext.getCmp('otel').getValue();
                    var consignee = Ext.getCmp('consignee').getValue();
                    var ctel = Ext.getCmp('ctel').getValue();
                    if (orderer == '' || otel == '' || consignee == '' || ctel == '') {
                        Ext.Viewport.unmask();
                        cordova.exec(function (message) {
                        }, function (error) {
                        }, "Messages", "showMsg", ['订货人和收货人信息不能为空！']);
//                        Ext.Msg.alert('订货人和收货人信息不能为空！');
                        return false;
                    }
                    if (!((/^13\d{9}$/g.test(otel)) || (/^15[0-35-9]\d{8}$/g.test(otel)) || (/^18[0-35-9]\d{8}$/g.test(otel)))) {
                        Ext.Viewport.unmask();
                        cordova.exec(function (message) {
                        }, function (error) {
                        }, "Messages", "showMsg", ['请填写订货人正确的手机号码！']);
//                        Ext.Msg.alert('请填写订货人正确的手机号码！');
                        return false;
                    }
                    if (!((/^13\d{9}$/g.test(ctel)) || (/^15[0-35-9]\d{8}$/g.test(ctel)) || (/^18[0-35-9]\d{8}$/g.test(ctel)))) {
                        Ext.Viewport.unmask();
                        cordova.exec(function (message) {
                        }, function (error) {
                        }, "Messages", "showMsg", ['请填写收货人正确的手机号码！']);
//                        Ext.Msg.alert('请填写收货人正确的手机号码！');
                        return false;
                    }
                    this.getOrdersFrom().submit({
                        url: Global.api_url + '/cloud/1/orders_add',
                        method: 'POST',
                        scope: this,
                        success: function (from, result) {
                            Ext.Viewport.unmask();
                            cordova.exec(function (message) {
                            }, function (error) {
                            }, "Messages", "showMsg", [result.Variables.Message.messagestr]);
//                            Ext.Msg.alert(result.Variables.Message.messagestr);
                            var windowse=this;
                            if (result.Variables.Result.code == 0) {
                                Ext.Viewport.mask({ xtype: 'loadmask' });
                                if(this.getPaymentTypes().getGroupValue()=='在线支付'){
                                    var ref = window.open(Global.api_url+'/cloud/1/cloud_pay_alipay?out_trade_no='+Global.app_id+'_'+window.localStorage.getItem('app_Login_id')+'_'+result.Variables.data, '_blank', 'location=no');
                                    ref.addEventListener('loadstop', function(event) {
                                        if (event.url.match("cloud_pay_index")||event.url.match("cloud_pay_back_url")) {
                                            ref.close();
                                            Ext.Viewport.mask({xtype: 'loadmask'});
                                            Ext.Viewport.remove(windowse.getShoppingBalanceView(), true);
                                            var main =windowse.getMainView();
                                            var users=windowse.getUsersView();
                                            main.setActiveItem(users);
                                            Ext.Viewport.animateActiveItem('usersCurrent', {type: 'slide', direction: 'left'});
                                        }
                                    },this);
                                    ref.addEventListener('exit', function(event) {
                                        Ext.Viewport.mask({xtype: 'loadmask'});
                                        Ext.Viewport.remove(windowse.getShoppingBalanceView(), true);
                                        var main =windowse.getMainView();
                                        var users=windowse.getUsersView();
                                        main.setActiveItem(users);
                                        Ext.Viewport.animateActiveItem('usersCurrent', {type: 'slide', direction: 'left'});
                                    },this);
                                } else{
                                    Ext.Viewport.remove(this.getShoppingBalanceView(), true);
                                    var main =this.getMainView();
                                    var users=this.getUsersView();
                                    main.setActiveItem(users);
                                    Ext.Viewport.animateActiveItem('usersCurrent', {type: 'slide', direction: 'left'});
                                }
                            }
                        },
                        failure: function () {
                            Ext.Viewport.unmask();
                            cordova.exec(function (message) {
                            }, function (error) {
                            }, "Messages", "showMsg", ['请求失败,服务器维护中...']);
                            // Ext.Msg.alert('请求失败', '服务器维护中....');
                        }
                    });
                }
            }
        }
    }

});